/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package isratrzabality.datalayer;

import isratrzabality.uilayer.FileChooseTrazability;
import isratrzabality.uilayer.Trazability;
import java.io.BufferedReader;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Israel Ruiz
 */
public class DataSourceMemory {

    private static Map<String, List<Artefact>> arftefactMemory = new HashMap<String, List<Artefact>>();
    private static String parentKey;
    private static int indx = 1;

    public static void initFilesToPersisten(JList jList1, Trazability frame) {
        DefaultListModel listModel = (DefaultListModel) jList1.getModel();
        JButton jButton2 = frame.getjButton2();
        JButton jButton3 = frame.getjButton3();
        if (listModel.getSize() < 1) {
            JOptionPane.showMessageDialog(frame, "Hace falta archivos para procesar", "Warning!!", JOptionPane.WARNING_MESSAGE);
            return;
        }
        for (int i = 0; i < listModel.getSize(); i++) {
            FileChooseTrazability fileInList = (FileChooseTrazability) listModel.elementAt(i);
            System.out.println(fileInList.getFile());
            try {
                ReadsFile(fileInList.getFile());
            } catch (IOException ex) {
                Logger.getLogger(DataSourceMemory.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        jButton2.setEnabled(false);
        jButton3.setEnabled(true);
    }

    private static void ReadsFile(File fileName) throws IOException {

        List<Artefact> artefacts = new ArrayList<Artefact>();
        

        String line = "";
        String splitLine[];
        String splitRefe[];
        BufferedReader buffLine = new BufferedReader(new FileReader(fileName));
        buffLine.readLine(); // header file

        while ((line = buffLine.readLine()) != null) {
            splitLine = line.split("\\|");
            if (1 < splitLine.length) {
                String id = splitLine[0];
                String name = splitLine[1];
                Artefact artefact = new Artefact(id, name);
                if (splitLine.length > 2) {
                    splitRefe = splitLine[2].split("\\,");
                    for (String idRef : splitRefe) {
                        Artefact artefactRef = findArtefactById(idRef);
                        if(artefactRef!= null){
                            artefact.addArtefactUp(artefactRef);
                            artefactRef.addArtefactDown(artefact);
                        }
                    }
                }
                artefacts.add(artefact);
            }
        }
        parentKey = "0"+(indx++)+"-"+fileName.getName().substring(0,fileName.getName().indexOf(".txt"));
        getArftefactMemory().put(parentKey, artefacts);

    }

    private static Artefact findArtefactById(String id) {

        List<Artefact> references = getArftefactMemory().get(parentKey);

        for (Artefact art : references) {
            if (art.getId().equals(id)) {
                return art;
            }
        }
        return null;
    }

    /**
     * @return the arftefactMemory
     */
    public static Map<String, List<Artefact>> getArftefactMemory() {
        return arftefactMemory;
    }
}

